package org.devgframwork.datasync.admin.support.init.impl;

import org.devgframwork.datasync.admin.support.init.AbstractInit;
import org.devgframwork.datasync.core.entity.SqlConfig;
import org.devgframwork.datasync.core.support.DataSourceMemory;
import org.devgframwork.datasync.core.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.devgframwork.datasync.admin.rabbit.service.SqlConfigService;

import java.util.List;

/**
 * @Description
 * @auther dh
 * @date 2022/1/5 15:14
 */
@Slf4j
public class SqlInit extends AbstractInit {

    public SqlInit(AbstractInit nextAbstractInit) {
        super(nextAbstractInit);
    }

    @Override
    public void initComponent() {
        SqlConfigService sqlConfigService = DataSourceMemory.applicationContext.getBean(SqlConfigService.class);
        List<SqlConfig> sqlConfigList = sqlConfigService.getSqlConfigList();
        if (CommonUtil.isEmpty(sqlConfigList)) {
            log.warn("sqlConfigList is empty");
            return;
        }
        for (SqlConfig sqlConfig : sqlConfigList) {
            String keyName = sqlConfig.getKeyName();
            String sqlText = sqlConfig.getSqlText();
            DataSourceMemory.sqlMap.put(keyName, sqlText);
        }
        log.info("init sql success");
    }
}
